package com.tac.se2_project2;
import java.util.ArrayList;

import com.tac.se2_project2.MySqlDB;

import android.os.AsyncTask;


public class CloudHelper {
	Contact[] contacts;
	Category[] cats;
	public int CheckUsernamePassword(String u, String p){
		CheckCloudUsernamePassword task = new CheckCloudUsernamePassword();
		task.execute(u, p);
		int check = -1;
		try {
			check = task.get();
		}
		catch (Exception e) {
			check = -1;
		}
		
		return check;
	}
	
	public boolean CheckUsernameUnique(String username){
		CheckCloudUsername task = new CheckCloudUsername();
		task.execute(username);
		boolean check = true;
		try {
			check = task.get();
		}
		catch (Exception e) {
			check = true;
		}
		
		return check;
	}
	
	public int InsertNewUser(String username, String password, String salt){
		InsertNewCloudUser task = new InsertNewCloudUser();
		task.execute(username, password, salt);
		int userId = -1;
		try {
			userId = task.get();
		}
		catch (Exception e) {
			userId = -1;
		}
		
		return userId;
	}
	
	public void UploadInformation(Contact[] contacts, Category[] cats, int userId){
		for(Contact c : contacts){
			c.set_userId(userId);
		}
		for(Category c : cats){
			c.set_userId(userId);
		}
		this.contacts = contacts;
		this.cats = cats;
		new RemoveData().execute(userId);
		
	}
	
	public ContactsCategories  RetreiveInformation(int userId){
		RetreiveCategories catTask = new RetreiveCategories();
		RetreiveContacts contTask = new RetreiveContacts();
		Contact[] conts;
		Category[] cats;
		contTask.execute(userId);
		try {
			conts = contTask.get();
		}
		catch (Exception e) {
			conts = null;
		}
		catTask.execute(userId);
		try {
			cats = catTask.get();
		}
		catch (Exception e) {
			cats = null;
		}
		ContactsCategories model = new ContactsCategories();
		model.categories = cats;
		model.contacts = conts;
		return model;
		
	}
	
	public class RetreiveCategories extends AsyncTask<Integer, Void, Category[]> {
		@Override
		protected Category[] doInBackground(Integer... params) {
			return new MySqlDB().GetAllCategories(params[0]);
		}
	}
	
	public class RetreiveContacts extends AsyncTask<Integer, Void, Contact[]> {
		@Override
		protected Contact[] doInBackground(Integer... params) {
			return new MySqlDB().GetAllContacts(params[0]);
		}
	}
	
	public class BackupAllContact extends AsyncTask<Contact, Void, Void> {
		@Override
		protected Void doInBackground(Contact... params) {
			new MySqlDB().BackupAllContacts(params);
			return null;
		}
	}
	
	public class BackupAllCategory extends AsyncTask<Category, Void, Void> {
		@Override
		protected Void doInBackground(Category... params) {
			new MySqlDB().BackupAllCategories(params);
			return null;
		}
	}
	
	
	public class RemoveData extends AsyncTask<Integer, Void, Void> {
		@Override
		protected Void doInBackground(Integer... params) {
			new MySqlDB().RemoveData(params[0]);
			new BackupAllCategory().execute(cats);
			new BackupAllContact().execute(contacts);
			return null;
		}
	}
	
	//Async task for checking if user has a unique username
	public class CheckCloudUsername extends AsyncTask<String, Void, Boolean> {
		@Override
		protected Boolean doInBackground(String... params) {
			return new MySqlDB().CheckUsernameExists(params[0]);
		}
	}
	
	public class CheckCloudUsernamePassword extends AsyncTask<String, Void, Integer> {
		@Override
		protected Integer doInBackground(String... params) {
			return new MySqlDB().CheckUsernamePasswordValid(params[0], params[1]);
		}
	}
	
	//Async task for inserting a new cloud user
		public class InsertNewCloudUser extends AsyncTask<String, Void, Integer> {
			@Override
			protected Integer doInBackground(String... params) {
				return new MySqlDB().InsertNewUser(params[0], params[1], params[2]);
			}
		}
}
